package com.shuoyue.ycgk.ui.estimate;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shuoyue.ycgk.R;

import java.util.List;

public class SpinnerAdapter<T> extends BaseAdapter {

    List<T> mData;
    Context context;
    LayoutInflater mInflater;

    public SpinnerAdapter(List<T> mData, Context context) {
        this.mData = mData;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_spinner_item,
                    null);
            holder = new ViewHolder();
            holder.textView = convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if(mData.get(position)!=null) {
            holder.textView.setText(mData.get(position).toString());
        }else {
            holder.textView.setText("请选择");
        }
        return convertView;

    }

    class ViewHolder {
        TextView textView;
    }

    public void resetData(List<T> newData) {
        this.mData = newData;
        notifyDataSetChanged();
    }
}
